package cn.com.cis.web.actions;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap;

import cn.com.cis.domain.GlobalParam;
import cn.com.cis.domain.GlobalParamGroup;
import cn.com.cis.plugins.mybatis.PageInfo;
import cn.com.cis.service.GlobalParamService;

/**
 * 
 * @author Haishen Hu
 * @2015年10月28日
 */
@Controller
@RequestMapping(value = "/parameter")
public class GlobalParameterController {
	
	@Autowired
	private GlobalParamService globalParamService;
	
	private void setNavigateBar(ModelAndView mv){
    	mv.addObject("icon", "icon-list");
        mv.addObject("title", "全局参数管理");
    }
	
	/**
	 * 加载全局参数列表
	 * @return
	 */
	@RequiresPermissions("配置管理:全局参数管理:查看参数列表")
    @RequestMapping(value = "/pList", method = RequestMethod.GET)
	public ModelAndView pList(@RequestParam(value = "page", defaultValue = "1", required = false) int page,
            				  @RequestParam(value = "pageSize", defaultValue = "5", required = false) int pageSize) {
		ModelAndView mv = new ModelAndView("parameter/pList");
		this.setNavigateBar(mv);
		List<GlobalParam> paramList = globalParamService.findAllGParam(page,pageSize);
		List<GlobalParamGroup> groupList = globalParamService.findGParamGroupForPage(page,pageSize);
		
		PageInfo<GlobalParam>paramPageInfo = new PageInfo<GlobalParam>(paramList,pageSize);
		mv.addObject("paramList",paramList);
		mv.addObject("groupList",groupList);
		mv.addObject("page",page);
		mv.addObject("paramPageInfo", paramPageInfo);
		return mv;
	}
	
	/**
	 * 根据组ID加载全局参数
	 * @param groupId
	 * @return
	 */
	@RequiresPermissions("配置管理:全局参数管理:根据组ID查询参数列表")
	@RequestMapping(value = "/pListForGroupId",method = RequestMethod.GET, produces = "application/json")
	@ResponseBody
	public List<GlobalParam> pListForGroupId(int groupId) {
		return globalParamService.findGParamByGroupId(groupId);
	}
	
	/**
	 * 跳转到组别添加页面
	 * @param group
	 * @return
	 */
	@RequiresPermissions("配置管理：全局参数管理：添加组别页面")
	@RequestMapping(value = "/toAddGroup",method = RequestMethod.GET, produces = "application/json")
	public ModelAndView toAddGroup() {
		ModelAndView mv = new ModelAndView("parameter/pAddGroup");
		this.setNavigateBar(mv);
		return mv;
	}
	
	/**
	 * 保存组
	 * @param group
	 * @return
	 */
	@RequiresPermissions("配置管理：全局参数管理：添加组")
	@RequestMapping(value = "/saveGroup",method = RequestMethod.POST)
	public ModelAndView saveGroup(GlobalParamGroup group,HttpServletRequest request) {
		
		if (0 < globalParamService.findGParamGroupByName(group.getName()).size()) {
			ModelAndView mv = new ModelAndView();
			this.setNavigateBar(mv);
			
			HttpSession session = request.getSession(); 
			session.setAttribute("error", "组"+group.getName()+"已经存在，请重新填写！");
			session.setAttribute("groupName",""+group.getName());
			session.setAttribute("groupRemark",""+group.getRemark());
			
			mv.setViewName("redirect:/parameter/toAddGroup");
			return mv;
		}
		
		ModelAndView mv = new ModelAndView("parameter/plist");
		this.setNavigateBar(mv);
		
		
		globalParamService.addGParamGroup(group);
		return this.pList(1,5);
	}
	
	/**
	 * 删除组
	 * @param groupId
	 * @param page
	 * @param modelMap
	 * @return
	 */
	@RequiresPermissions("配置管理：全局参数管理：删除组")
    @RequestMapping(value = "/delGroup")
	@ResponseBody
	public ModelAndView delGroup(int groupId,@RequestParam(value = "page",required = true,defaultValue = "1")int page,
			HttpServletRequest request) {
		ModelAndView mv = new ModelAndView("redirect:/parameter/pList");
		
		if (null != globalParamService.findGParamByGroupId(groupId)) {
			HttpSession session = request.getSession(); 
			session.setAttribute("error", "该组下存在全局参数，不能删除！");
			return mv;
		}
        
        globalParamService.delGParamGroupById(groupId);
        
		return mv;
	}
	
	/**
	 * 进入组修改页面
	 * @param groupId
	 * @return
	 */
	@RequiresPermissions("配置管理：全局参数管理：修改组页面")
	@RequestMapping(value = "/toEditGroup",method = RequestMethod.GET, produces = "application/json")
	public ModelAndView toEditGroup(int groupId) {
		ModelAndView mv = new ModelAndView("parameter/pEditGroup");
		setNavigateBar(mv);
		
		GlobalParamGroup group = globalParamService.findGParamGroupById(groupId);
		mv.addObject("id", group.getId());
		mv.addObject("name", group.getName());
		mv.addObject("remark", group.getRemark());
		return mv;
	}
	
	/**
	 * 修改组
	 * @param paramGroup
	 * @return
	 */
	@RequiresPermissions("配置管理：全局参数管理：修改组")
	@RequestMapping(value = "/updateGroup",method = RequestMethod.POST)
	public ModelAndView updateGroup(GlobalParamGroup paramGroup) {
		ModelAndView mv = new ModelAndView("parameter/pList");
		setNavigateBar(mv);
		globalParamService.modiGParamGroup(paramGroup);
		return this.pList(1, 5);
	}
	
	/**
	 * 跳转到参数添加页面
	 * @return
	 */
	@RequiresPermissions("配置管理：全局参数管理：添加参数页面")
	@RequestMapping(value = "/toAddParam",method = RequestMethod.GET, produces = "application/json")
	public ModelAndView toAddParamPage() {
		ModelAndView mv = new ModelAndView("parameter/pAddParam");
		setNavigateBar(mv);
		
		mv.addObject("groupList", globalParamService.findAllGParamGroup());
		
		return mv;
	}
	
	/**
	 * 保存参数
	 * @param globalParam
	 * @param modelMap
	 * @return
	 */
	@RequiresPermissions("配置管理：全局参数管理：保存参数")
	@RequestMapping(value = "/saveParam",method = RequestMethod.POST)
	@ResponseBody
	public ModelAndView saveParam(GlobalParam globalParam,HttpServletRequest request) {
		HttpSession session = request.getSession();
		if (0 < globalParamService.findGParamByName(globalParam.getgParamName()).size()) {
			ModelAndView mv = new ModelAndView("parameter/pAddParam");
			session.setAttribute("globalParam", globalParam);
			session.setAttribute("error", "全局参数"+globalParam.getgParamName()+"已经存在，请重新输入！");
			return mv;
		}
		globalParamService.addGParam(globalParam);
		session.setAttribute("msg", "添加参数成功！");
		return this.pList(1, 5);
	}
	
	/**
	 * 删除参数
	 * @param paramId
	 * @param modelMap
	 * @return
	 */
	@RequiresPermissions("配置管理：全局参数管理：删除参数")
	@RequestMapping(value = "/delParam")
	public ModelAndView delParam(int paramId,RedirectAttributesModelMap modelMap) {
		globalParamService.delGParamById(paramId);
		modelMap.addAttribute("msg", "删除成功！");
		return this.pList(1, 5);
	}
	
	/**
	 * 进入修改页面
	 * @param paramId
	 * @return
	 */
	@RequiresPermissions("配置管理：全局参数管理：修改参数页面")
	@RequestMapping(value = "/toEditParam",method = RequestMethod.GET, produces = "application/json")
	public ModelAndView toEditParamPage(int paramId) {
		ModelAndView mv = new ModelAndView("parameter/pEdit");
		setNavigateBar(mv);
		
		GlobalParam param = globalParamService.findGParamById(paramId);
		GlobalParamGroup group = globalParamService.findGParamGroupById(param.getGroup().getId());
		
		mv.addObject("paramId", param.getId());
		mv.addObject("paramName", param.getgParamName());
		mv.addObject("paramType", param.getgParamType());
		mv.addObject("paramValue", param.getgParamValue());
		mv.addObject("paramRemark", param.getgParamRemark());
		mv.addObject("group", group);
		mv.addObject("groupList", globalParamService.findAllGParamGroup());
		
		
		return mv;
	}
	
	/**
	 * 修改参数信息
	 * @param param
	 * @return
	 */
	@RequiresPermissions("配置管理：全局参数管理：修改参数")
	@RequestMapping(value = "/updateParam",method = RequestMethod.POST)
	public ModelAndView updateParam(GlobalParam param) {
		
		globalParamService.modiGParam(param);
		return this.pList(1, 5);
	}

}
